package com.laopan.elasticsearch.core;

import com.laopan.elasticsearch.bean.Field;
import com.laopan.elasticsearch.bean.Page;
import com.laopan.elasticsearch.core.geo.GeoQuery;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * 文档查询
 * @author laopan
 * @className EsQuery
 * @date 2021/4/20 11:14
 */

public class Query {
    private static final Logger log = LoggerFactory.getLogger(GeoQuery.class);
    private RestHighLevelClient highLevelClient;

    public Query(RestHighLevelClient highLevelClient) {
        this.highLevelClient = highLevelClient;
    }

    public SearchResponse searchRequest(SearchRequest request, SearchSourceBuilder builder) {
        request.source(builder);
        try {
            return highLevelClient.search(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("query data Exception", e);
        }
        return null;
    }

    /**
     * id查询
     * @param index
     * @param id
     * @return
     */
    public SearchResponse idQuery(String index, String id) {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.idsQuery().addIds(id));
        return searchRequest(request, sourceBuilder);
    }
    /**
     * 单字段查询
     * @param index
     * @param fieldName
     * @param fieldData
     * @return
     */
    public SearchResponse termQuery(String index, Field field,Page page) {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery(field.getName(), field.getData()));
        if(null!=page) {
            sourceBuilder.from(page.getPageNum()).size(page.getPageSize());
        }
        return searchRequest(request, sourceBuilder);
    }
    public SearchResponse termQuery(String index, Field field) {
        return termQuery(index, field,null);
    }
    public SearchResponse multiTermQuery(String index, List<Field> field) {
        return multiTermQuery(index, field,null);
    }
    public SearchResponse multiTermQuery(String index, List<Field> field,Page page) {
        SearchRequest request = new SearchRequest(index);
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        if(null==field||field.isEmpty()){
            return null;
        }
        field.forEach(data -> query.must(QueryBuilders.termQuery(data.getName(), data.getData())));
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(query);
        if(null!=page) {
            builder.from(page.getPageNum()).size(page.getPageSize());
        }
        return searchRequest(request, builder);
    }
    public SearchResponse search(String index, String[] fieldNames , String searchData) {
        return search(index, fieldNames,searchData,null);
    }
    public SearchResponse search(String index, String[] fieldNames , String searchData,Page page) {
        SearchRequest request = new SearchRequest(index);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        if (null == fieldNames || 0 == fieldNames.length) {
            throw new NullPointerException("查询的字段名称不为空");
        }
        if (null != searchData && searchData.trim().length() > 0) {
            builder.query(QueryBuilders.multiMatchQuery(searchData, fieldNames));
        }
        if(null!=page) {
            builder.from(page.getPageNum()).size(page.getPageSize());
        }
        return searchRequest(request, builder);
    }

}
